package com.max.common.oauth.oauth20;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.max.common.proxy.ServiceProxyFactory;
import com.max.common.util.StringUtil;

@SuppressWarnings("serial")
public class OAuth20Servlet extends HttpServlet 
{

	@Override
	public void init(ServletConfig config) throws ServletException 
	{
		super.init(config);
	}

	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		final String code = request.getParameter("code");
//		final String clientName = "microblog_sina_01";
		final String clientName = "microblog_tencent_01";
		
		if(StringUtil.isEmpty(code))
		{
			String authorizeURL = Oauth20.authorize(clientName, "code");
			response.sendRedirect(authorizeURL);
			return;
		}
		else
		{
			try 
			{
				AccessToken accessToken = Oauth20.getAccessTokenByCode(clientName, code);
				ServiceProxyFactory factory = new ServiceProxyFactory();
//				MicroBlogSinaImpl20 microBlogSinaImpl20 = new MicroBlogSinaImpl20();
//				microBlogSinaImpl20.setAccessToken(accessToken);
//				microBlogSinaImpl20.statuses_user_timeline(19824587, 0, 1, 50, 0);
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}
	
	
	@Override
	public void destroy()
	{
		super.destroy();
	}
}
